/*
 *所有关于cw_djlc_analyze_total类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*cw_djlc_analyze_totalService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Finance.IRepositories;
using System.Collections.Generic;
using System.Net;
using VOL.Core.DBManager;
using System;
using VOL.Core.ManageUser;

namespace VOL.Finance.Services
{
    public partial class cw_djlc_analyze_totalService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Icw_djlc_analyze_totalRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public cw_djlc_analyze_totalService(
            Icw_djlc_analyze_totalRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        readonly WebResponseContent webResponse = new WebResponseContent();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveModel)
        {
            
            UpdateOnExecuting = (cw_djlc_analyze_total cw_djlc_analyze_total, object addList, object updateList, List<object> delKeys) =>
            {
                using (var db = DBServerProvider.DbContext)
                {
                    cw_djlc_analyze_reason cw_djlc_analyze_reason = DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Where(x => x.cw_djlc_djzb_id == cw_djlc_analyze_total.cw_djlc_djzb_id && x.groupnum == cw_djlc_analyze_total.groupnum).FirstOrDefault();
                    if (cw_djlc_analyze_reason.IsNullOrEmpty())
                    {
                        cw_djlc_djzb cw_djlc_djzb = DBServerProvider.DbContext.Set<cw_djlc_djzb>().Where(x=>x.cw_djlc_djzb_id == cw_djlc_analyze_total.cw_djlc_djzb_id).FirstOrDefault();
                        cw_djlc_analyze_reason cw_djlc_analyze_reason1 = new cw_djlc_analyze_reason();
                        cw_djlc_analyze_reason1.cw_djlc_diff_analyze_id = Guid.NewGuid();
                        cw_djlc_analyze_reason1.cw_djlc_djzb_id = cw_djlc_analyze_total.cw_djlc_djzb_id;
                        cw_djlc_analyze_reason1.cw_djlc_code = cw_djlc_djzb.cw_djlc_code;
                        cw_djlc_analyze_reason1.groupnum = cw_djlc_analyze_total.groupnum;
                        cw_djlc_analyze_reason1.groupnumname = cw_djlc_analyze_total.groupnumname;
                        cw_djlc_analyze_reason1.is_rational = cw_djlc_analyze_total.is_rational;
                        cw_djlc_analyze_reason1.reason = cw_djlc_analyze_total.reason;
                        cw_djlc_analyze_reason1.reasonfileurl = cw_djlc_analyze_total.reasonfileurl;
                        cw_djlc_analyze_reason1.Creator = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                        cw_djlc_analyze_reason1.CreateDate = DateTime.Now;
                        DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Add(cw_djlc_analyze_reason1);
                    }
                    else
                    {
                        cw_djlc_analyze_reason.is_rational = cw_djlc_analyze_total.is_rational;
                        cw_djlc_analyze_reason.reason = cw_djlc_analyze_total.reason;
                        cw_djlc_analyze_reason.reasonfileurl = cw_djlc_analyze_total.reasonfileurl;;
                        cw_djlc_analyze_reason.Modifier = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                        cw_djlc_analyze_reason.ModifyDate = DateTime.Now;
                        DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Update(cw_djlc_analyze_reason);
                    }

                    DBServerProvider.DbContext.SaveChanges();
                }
                webResponse.Code = "-1";
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }

        public override PageGridData<cw_djlc_analyze_total> GetPageData(PageDataOptions options)
        {
            SummaryExpress = (IQueryable<cw_djlc_analyze_total> queryable) =>
            {
                return (queryable as IQueryable<cw_djlc_analyze_total>).GroupBy(x => 0).Select(x => new
                {
                    //AvgPrice注意大小写和数据库字段大小写一样
                    oldprice = x.Sum(o => o.oldprice),
                    newprice = x.Sum(o => o.newprice),
                    analyzeprice = x.Sum(o => o.analyzeprice)
                }).FirstOrDefault();

            };
            return base.GetPageData(options);
        }
        /// <summary>
        /// 重新导出方法
        /// </summary>
        /// <param name="pageData"></param>
        /// <returns></returns>
        public override WebResponseContent Export(PageDataOptions pageData)
        {
            ExportColumns = x => new {
                x.cw_djlc_code,
                x.groupnum,
                x.groupnumname,
                x.analyzeprice,
                x.is_rational,
                x.reason,
                x.caigou_is_rational,
                x.feedback,
            };
            return base.Export(pageData);
        }

        /// <summary>
        /// 导入方法
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public override WebResponseContent Import(List<IFormFile> files)
        {
            DownLoadTemplateColumns = x => new {
                x.cw_djlc_code,
                x.groupnum,
                x.groupnumname,
                x.analyzeprice,
                x.is_rational,
                x.reason,
                x.caigou_is_rational,
                x.feedback,
            };
            ImportOnExecuting = (List<cw_djlc_analyze_total> cw_djlc_analyze_total) =>
            {

                foreach (var item in cw_djlc_analyze_total)
                {
                    /// 只要四个值其中有一个就需要存入
                    if (!item.is_rational.IsNullOrEmpty() || !item.reason.IsNullOrEmpty() 
                     || !item.caigou_is_rational.IsNullOrEmpty() || !item.feedback.IsNullOrEmpty())
                    {
                        cw_djlc_analyze_reason cw_djlc_analyze_reason = DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Where(x => x.cw_djlc_code == item.cw_djlc_code && x.groupnum == item.groupnum).FirstOrDefault();

                        if (cw_djlc_analyze_reason.IsNullOrEmpty())
                        {
                            cw_djlc_djzb cw_djlc_djzb = DBServerProvider.DbContext.Set<cw_djlc_djzb>().Where(x => x.cw_djlc_code == item.cw_djlc_code).FirstOrDefault();
                            cw_djlc_analyze_reason cw_djlc_analyze_reason1 = new cw_djlc_analyze_reason();
                            cw_djlc_analyze_reason1.cw_djlc_diff_analyze_id = Guid.NewGuid();
                            cw_djlc_analyze_reason1.cw_djlc_djzb_id = cw_djlc_djzb.cw_djlc_djzb_id;
                            cw_djlc_analyze_reason1.cw_djlc_code = item.cw_djlc_code;
                            cw_djlc_analyze_reason1.groupnum = item.groupnum;
                            cw_djlc_analyze_reason1.groupnumname = item.groupnumname;
                            cw_djlc_analyze_reason1.is_rational = item.is_rational;
                            cw_djlc_analyze_reason1.reason = item.reason;
                            cw_djlc_analyze_reason1.Creator = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                            cw_djlc_analyze_reason1.CreateDate = DateTime.Now;
                            DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Add(cw_djlc_analyze_reason1);
                        }
                        else
                        {
                            cw_djlc_analyze_reason.is_rational = item.is_rational;
                            cw_djlc_analyze_reason.reason = item.reason;
                            cw_djlc_analyze_reason.Modifier = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                            cw_djlc_analyze_reason.ModifyDate = DateTime.Now;
                            DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Update(cw_djlc_analyze_reason);
                        }

                        DBServerProvider.DbContext.SaveChanges();
                    }
                   
                }
                webResponse.Code = "-1";
                return webResponse.OK("修改成功！");
            };
            return base.Import(files);
        }

        public override WebResponseContent DownLoadTemplate()
        {
            DownLoadTemplateColumns = x => new {
                x.cw_djlc_code,
                x.groupnum,
                x.groupnumname,
                x.analyzeprice,
                x.is_rational,
                x.reason,
                x.caigou_is_rational,
                x.feedback,
            };
            return base.DownLoadTemplate();
        }

    }
}
